package anc.server.tankshooter.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import anc.server.tankshooter.App;
import anc.server.tankshooter.models.Account;
import anc.server.tankshooter.models.IEntity;
import anc.server.tankshooter.models.Statistic;

public class StatisticManager extends IManager {

	public StatisticManager(Connection connection) {
		super(connection, Connect.STATISTIC_TABLE);
	}

	/**
	 * @param account
	 * @return Trả về id của row đc thêm vào. -1 nếu thất bại
	 */
	@Override
	public int add(IEntity entity) {
		
		int newId = -1;
		try{
			Statistic statistic = (Statistic)entity;
			String sql ="INSERT INTO "+getTableName()+" VALUES(null,?,?,?)";
			PreparedStatement statement = mConnection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			statement.setInt(1, statistic.getMatchId());
			statement.setInt(2, statistic.getAccountId());
			statement.setInt(3, statistic.getScore());
			statement.execute();
			if(statement.getUpdateCount()>0){
				ResultSet reader = statement.getGeneratedKeys();
				if(reader.next()){
					newId = reader.getInt(1);
				}
				reader.close();
			}
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return newId;
	}

	
	@Override
	public boolean update(IEntity entity) {
		boolean isSuccess = false;
		try{
			Statistic statistic = (Statistic)entity;
			String sql ="UPDATE "+getTableName()+
						" SET match_id=?,account_id=?,score=?"+
						" WHERE statistic_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, statistic.getMatchId());
			statement.setInt(2, statistic.getAccountId());
			statement.setInt(3, statistic.getScore());
			statement.setInt(4, statistic.getStatisticId());
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	
	@Override
	public boolean delete(int accountId) {
		boolean isSuccess = false;
		try{
			String sql ="DELETE FROM "+getTableName()+" WHERE statistic_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, accountId);
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	
	@Override
	public boolean isExist(int accountId) {
		boolean isSuccess = false;
		try{
			String sql ="SELECT COUNT(*) FROM "+getTableName()+" WHERE statistic_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, accountId);
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	/**
	 * Đếm số lượng
	 */
	@Override
	public int count() {
		int counter = 0;
		try{
			String sql ="SELECT COUNT(*) FROM "+getTableName();
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.execute();
			counter = statement.getUpdateCount();
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return counter;
	}

}
